        <script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$("#tgl_isi").datepicker({
					dateFormat  : "yy-mm-dd",        
					  changeMonth : true,
					  changeYear  : true					
				});
				$("#id_penjualan").change(function(){
					$.ajax({						
						url:'getPesanan.php',
						dataType: "html",
						type:'POST',
						data:'id_penjualan='+$(this).val(),
						success: function(data) {
							var obj = $.parseJSON(data);						
							if(data=="[]"){
								$("#jumlah").val('');
										
							}else{							
								$("#jumlah").val(obj[0].jumlah);								
							}
						}
					})
				})
			} );
		</script>
<?php
if($_SESSION['breject']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Barang Broken</h5>								
						<div class="search global-nav">
						  <form method="GET" class="form-search js-search-form" action="index.php?pages=brg_broken" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="brg_broken">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="tgl_jual" <?php echo ($field=="satuan_rusak")?"selected":"";?>>Satuan Rusak</option>
									<option value="nota" <?php echo ($field=="satuan_kembali")?"selected":"";?>>Satuan Kembali</option>
								</select>
				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th class="left">Tgl Rusak</th>
									<th>Jenis Barang</th>
                                    <th>Jumlah Rusak Per Karton</th>
                                	<th>Satuan Rusak</th>
                                    <th>Jumlah Rusak Per Cup</th>
                                    <th>Jumlah Cup Ok</th>
                                	<th>Satuan Rusak</th>
									<th class="action" colspan="2"><a href="?pages=brg_broken&act=edit" class="icon-plus-sign" title="Tambah Data Baru"></a></th>									
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;
							if($field=='status'){
								$q2 = (strtolower($q)=='aktif')?'1':'0';
							}
							
							$addQuery='';


							if(!empty($field))
								$addQuery .= " WHERE $field LIKE '%$q2%' ";

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlSup1 = mysql_query("SELECT * FROM tb_broken $addQuery ORDER BY id_broken DESC LIMIT $posisi, $batas");
							
							$no=$posisi+1;
							while($dataSup = mysql_fetch_array($sqlSup1)){								

								$status = ($dataSup['status']==1)? "Aktif":"Non Aktif";
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
                                     <?php 
										  $sql=mysql_query("select * from tb_broken where id_broken='$dataSup[id_broken]'");
										  $sql2=mysql_fetch_array($sql);
									?>
									<td><?php echo $sql2['tgl_isi'];?></td>
									<td><?php echo $dataSup['jumlah_rusak_dus'];?></td>
                                    <td><?php echo $dataSup['jumlah_rusak_dus'];?></td>
									<td><?php echo $dataSup['satuan_dus'];?></td>
                                    <td><?php echo $dataSup['jumlah_rusak_cup'];?></td>
                                    <td><?php echo $dataSup['jumlah_cup_ok'];?></td>
									<td><?php echo $dataSup['satuan_cup'];?></td>
                                    <td class="action"><a href="?pages=brg_broken&act=edit&id=<?php echo $dataSup['id_broken'];?>" class="icon-edit" title="Ubah Data"></a></td>
									<td class="action">
										<a href="#" title="Hapus Data" onClick="if(confirm('Anda akan menghapus data ini?')) window.location='?pages=brg_broken&act=del&id=<?php echo $dataSup['id_broken'];?>';" class="icon-trash"></a>
									</td>								
								</tr>
							<?php	
								$no++;				
							}

							$sqlSup2 = mysql_query("SELECT * FROM tb_broken $addQuery ORDER BY id_broken DESC");
							$jumlahData = mysql_num_rows($sqlSup2);

							$file="?pages=brg_broken&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
					  </div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					</div>

<?php
}elseif ($act=='edit') {

	$id_broken = (int) filter($_GET['id']);

	$sqlSup = mysql_query("SELECT * FROM tb_broken WHERE id_broken=$id_broken");
	$dataSup = mysql_fetch_array($sqlSup);

	if(!empty($id_broken)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Data Barang Broken</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Data Barang Broken</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<form method="POST" action="?pages=brg_broken&act=update">
		<input type="hidden" name="id_broken" value="<?php echo $dataSup['id_broken'];?>">
		<table style="width:auto">
		   	<tbody>
                <?php 
                	$juml=mysql_query("SELECT * FROM tb_broken WHERE id_broken='$dataSup[id_broken]' ");
                	$juml2=mysql_fetch_array($juml);
                ?>
                <tr>
					<td><label>Tanggal</label> </td>
					<td>:</td>
					<td><input type="text"  value="<?php echo $dataSup['tgl_isi'];?>" size="30" name="tgl_isi" id="tgl_isi" placeholder="Tgl. Input" required="required"/></td>
			   	</tr>
                 <tr>
					<td><label>Jumlah Rusak Karton</label></td>
					<td>:</td>
				 	<td><select class="chzn-select" style="width: 210px;" name="id_jns_brg" id="id_jns_brg"  data-placeholder="Pilih Jenis Barang" required="required">
											  <option value=""></option>
							<?php
								$selectJns = mysql_query("SELECT * FROM  tb_jns_barang");
								while($dataJns = mysql_fetch_array($selectJns)){
								if($dataSup['id_jns_brg']==$dataJns['id_jns_brg']){
								$selected = " selected ";
								}else{
								$selected = "";
								}
								?>
								<option <?php echo $selected;?> value="<?php echo "$dataJns[id_jns_brg]";?>"><?php echo "$dataJns[kode_brg]=>$dataJns[nama_brg]";?></option>
                            <?php
                            }
                            ?>
										    </select>
                    
                    <input type="hidden" value="<?php echo $dataSup['jumlah_rusak_dus'];?>" size="30"name="jumlah_rusak_dusx" id="jumlah_rusak_dus" placeholder="jumlah rusak Karton" required="required"/></td> 
			   	</tr>
                 <tr>
					<td><label>Jumlah Rusak Karton</label></td>
					<td>:</td>
				 	<td><input type="text" value="<?php echo $dataSup['jumlah_rusak_dus'];?>" size="30" name="jumlah_rusak_dus" id="jumlah_rusak_dus" placeholder="jumlah rusak Karton" required/>
                    
                    <input type="hidden" value="<?php echo $dataSup['jumlah_rusak_dus'];?>" size="30"name="jumlah_rusak_dusx" id="jumlah_rusak_dus" placeholder="jumlah rusak Karton" required="required"/></td> 
			   	</tr>
                
                <tr>
					<td><label>Satuan Rusak Karton</label></td>
					<td>:</td>
				 	<td><input type="text" value="Karton" size="30" name="satuan_dus" id="satuan" placeholder="satuan rusak Karton" readonly/></td> 
			   	</tr>	
                <tr>
					<td><label>Jumlah Cup Rusak</label></td>
					<td>:</td>
				 	<td><input type="text" value="<?php echo $dataSup['jumlah_rusak_cup'];?>" size="30" name="jumlah_rusak_cup" id="jumlah_rusak_cup" placeholder="jumlah rusak cup" required/>
                    
                     <input type="hidden"  value="<?php echo $dataSup['jumlah_rusak_cup'];?>" size="30" name="jumlah_rusak_cupx" id="jumlah_rusak_cup" placeholder="jumlah rusak cup" required="required"/></td> 
			   	</tr>
                <td><label>Jumlah  Cup Ok</label></td>
					<td>:</td>
				 	<td><input type="text" value="<?php echo $dataSup['jumlah_cup_ok'];?>" size="30" name="jumlah_cup_ok" id="jumlah_cup_ok" placeholder="jumlah cup ok" required/>
                    
                    <input type="hidden" value="<?php echo $dataSup['jumlah_cup_ok'];?>" size="30" name="jumlah_cup_okx" id="jumlah_cup_okx" placeholder="jumlah cup ok" required/></td>
                <tr>
					<td><label>Satuan </label></td>
					<td>:</td>
				 	<td><input type="text" value="Cup" size="30" name="satuan_cup" id="satuan" placeholder="satuan rusak Cup" readonly/></td> 
			   	</tr>
             	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">
			 		</td>
		   		</tr>
		 	</tbody>
		</table>
		</form>
	</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_broken 			= (int) $_POST['id_broken'];
	$tgl_isi			=$_POST['tgl_isi'];
	$jumlah_rusak_dus	=$_POST['jumlah_rusak_dus'];
	$jumlah_rusak_dusx	=$_POST['jumlah_rusak_dusx'];
	$satuan_dus			=$_POST['satuan_dus'];
	$jumlah_rusak_cup	=$_POST['jumlah_rusak_cup'];
	$jumlah_rusak_cupx	=$_POST['jumlah_rusak_cupx'];
	$jumlah_cup_ok 		=$_POST['jumlah_cup_ok'];
	$jumlah_cup_okx 	=$_POST['jumlah_cup_okx'];
	$satuan_cup			=$_POST['satuan_cup'];
	$id_jns_brg			=$_POST['id_jns_brg'];

	$jum_temp = mysql_query("SELECT stock_rts from tb_temp_rts WHERE id_jns_brg='$id_jns_brg'");
		$jum_temp2 = mysql_fetch_array($jum_temp);
		$jum=$jum_temp2['stock_rts'];
		$hasil=$jum - $jumlah_rusak_dus;



		$update_temp_rts=mysql_query("SELECT * from tb_temp_rts WHERE id_jns_brg='$id_jns_brg'");
		$update_temp_rts2=mysql_fetch_array($update_temp_rts);
		$temp2=$update_temp_rts2["stock_rts"];

		$total_temp=($temp2 + $jumlah_rusak_dusx)- $jumlah_rusak_dus;
// ketika add
	$stock_cup_sehat=mysql_query("SELECT stock_cup_sehat from tb_temp_rts WHERE id_jns_brg='$id_jns_brg'");
	$stock_cup_sehat2=mysql_fetch_array($stock_cup_sehat);
	$h=$stock_cup_sehat2["stock_cup_sehat"];

	$update_sehat=($h + $jumlah_cup_ok);

//edit
		$update_temp_rtsx=mysql_query("SELECT * from tb_temp_rts WHERE id_jns_brg='$id_jns_brg'");
		$update_temp_rtsx2=mysql_fetch_array($update_temp_rtsx);
		$tempx2=$update_temp_rtsx2["stock_cup_sehat"];

		$total_tempx=($tempx2 - $jumlah_cup_okx)+ $jumlah_cup_ok;

	if(!empty($id_broken)){	
		if($jumlah_rusak_dus > $jum ){
				sukses('Data Broken Karton anda melebihi jumlah Karton RTS', "?pages=brg_broken&act=edit&id=$id_broken");
			}
		else {
		
		$update = mysql_query("UPDATE tb_broken SET id_jns_brg='$id_jns_brg',tgl_isi='$tgl_isi',  jumlah_rusak_dus='$jumlah_rusak_dus', satuan_dus='$satuan_dus', 
		 jumlah_rusak_cup='$jumlah_rusak_cup',  jumlah_cup_ok='$jumlah_cup_ok', satuan_cup='$satuan_cup' WHERE id_broken=$id_broken");

		$update_temp=mysql_query("UPDATE tb_temp_rts SET stock_rts='$total_temp' WHERE id_jns_brg='$id_jns_brg'");

		$update3=mysql_query("UPDATE tb_temp_rts SET stock_cup_sehat='$total_tempx' WHERE id_jns_brg='$id_jns_brg'");
		//$update3=mysql_query("UPDATE tb_temp_rts SET stock_rts='$update_rts'");

		if($update && $update_temp && $update3)
			sukses('Data berhasil disimpan', '?pages=brg_broken');
}

}else{	

	if($jumlah_rusak_dus > $jum ){
				sukses('Data Broken Karton anda melebihi jumlah Karton RTS', "?pages=brg_broken&act=edit&id=$id_broken");
			}
		else {


		$insert = mysql_query("INSERT INTO tb_broken VALUES(NULL, '$id_jns_brg', '$tgl_isi', '$jumlah_rusak_dus', '$satuan_dus', 
			'$jumlah_rusak_cup','$jumlah_cup_ok', '$satuan_cup', '1')");
		$update2=mysql_query("UPDATE tb_temp_rts SET stock_cup_sehat='$update_sehat' WHERE id_jns_brg='$id_jns_brg'");
		$update_tempq=mysql_query("UPDATE tb_temp_rts SET stock_rts='$hasil' WHERE id_jns_brg='$id_jns_brg'");

		if($insert && $update2 && $update_tempq)
			sukses('Data berhasil disimpan', '?pages=brg_broken');
	}
}
}elseif ($act=='del') {
	# code...
	$id_broken = (int) filter($_GET['id']);

	$sql_cek = mysql_query("SELECT * from tb_broken where id_broken='$id_broken'");
	$cek = mysql_fetch_array($sql_cek);
	$jumlahnya = $cek['jumlah_cup_ok'];

	$temp_rts=mysql_query("SELECT stock_cup_sehat from tb_temp_rts WHERE id_jns_brg='$id_jns_brg'");
	$temp_rts2=mysql_fetch_array($temp_rts);
	$b3=$temp_rts2["stock_cup_sehat"];

	if ($cek['id_broken'] == true) {
		
		$total = $b3 - $jumlahnya;
		$sql_update_stok = mysql_query("UPDATE tb_temp_rts SET stock_cup_sehat = '$total' WHERE id_jns_brg='$id_jns_brg'");

	}

	$delete = mysql_query("DELETE FROM tb_broken WHERE id_broken=$id_broken");

	if($delete )
		sukses('Data berhasil dihapus', '?pages=brg_broken');
}
?>	

<link rel="stylesheet" href="css/chosen.css" />
<script src="js/chosen.jquery.js" type="text/javascript"></script>
<script type="text/javascript">
			function deleteAddress(e) {
				if($(e).parents(".tabelDetail").is('#detail_formula')){
					alert('Sisain satu ya..:p');
				}else{
					$(e).parents(".tabelDetail").remove();
				}
				
			}

			$(document).ready(function () {
				style_path = "css/colors";
				$('.dropdown-toggle').dropdown();
				//$("#date-picker").datepicker();

				//$("#box-tabs, #box-left-tabs").tabs();

				 $('.dropdown input, .dropdown label, .dropdown-menu').click(function(e) {
				    e.stopPropagation();
				 });

				  var cloneCount = 1;
				 $("#add_formula").click(function(e){
			         e.preventDefault();
				 	var clone = $('#detail_formula').clone().attr('id', 'detail_formula'+ cloneCount++).insertAfter($('[id^=detail_formula]:last'));
				 	clone.find(":input:not([readonly='readonly'])").val('');	
				 	clone.find('[type=hidden]').val('');
				 	clone.find('a.icon-minus-sign').attr('onClick','deleteAddress(this);return false');					 	
				 	$('#detail_formula'+(cloneCount-1)).find('.chzn-container').remove();
            		$('#detail_formula'+(cloneCount-1)).find('.chzn-select').removeClass('chzn-done').removeAttr('id').chosen({search_contains: true });
            		
            		$('#detail_formula'+(cloneCount-1)).find('#chzn-selectx1').removeClass('chzn-done').removeAttr('id').chosen({search_contains: true });
				 })

				$(".chzn-select").chosen({search_contains: true });  
				$("#id_sales").chosen({search_contains: true }); 
				$("#id_customer").chosen({search_contains: true });
				$("#id_jns_brg").chosen({search_contains: true });

$("#tgl_jual").datetimepicker({
					changeMonth: true,
				    changeYear: true,
				    showOtherMonths: true,
					dateFormat:'yy-mm-dd',
					timeFormat: 'hh:mm:ss',
			        hourGrid: 8,
			        minuteGrid: 10,		
				});
			});
			
</script>				